<?php

namespace App\Services\Purchase;

use App\Models\Purchase\PurchaseSettlePriceLog;
use App\Models\Steel\SteelMaterialPriceSupply;
use App\Models\Steel\SteelMaterialPriceSupplyHistory;
use Illuminate\Support\Facades\Redis;
use Illuminate\Http\Request;

class PurchaseCommon
{

    /**
     * 后结算取价,按照时间取平均值,取的是基价
     *
     * @param [type] $cpid
     * @param [type] $steel_factory_id--钢厂信息
     * @param [type] $stime--查询开始时间
     * @param [type] $etime--查询结束时间
     * @return void
     */
    public static function getsettleprice($cpid,$steel_factory_id,$stime,$etime){
        $base_price=PurchaseSettlePriceLog::where([['status',1],['is_del',1],['cpid',$cpid],['steel_factory_id',$steel_factory_id],['base_price','>',0],['update_at','>=',$stime],['update_at','<=',$etime]])
        ->avg('base_price');

        return $base_price;


    }

    /**
     * 获取价格--根据结算模式取价格
     *
     * @param [type] $material_id 材质id
     * @param [type] $archiveinfo 库存信息
     * @return void
     */
    public static function getprice($material_id, $stockinfo)
    {
        $supply_material_price = json_decode(Redis::get('supply_material_price_' . $material_id));
        if (!isset($supply_material_price['base_price'])) {
            $base_price = SteelMaterialPriceSupply::where('material_attr_id', $material_id)->value('base_price');
        } else {
            $base_price = $supply_material_price['base_price'];
        }


        if ($base_price <= 0) {
            return 0;
        } else {
            return intval(mathfunc('add', $base_price, $stockinfo['supply_add_price']));
        }
    }

    public static function getpricebysettle($material_id, $stockinfo, $cpid, $time = 0)
    {
        if ($time == 0) {
            $supply_material_price = json_decode(Redis::get('supply_material_price_' . $material_id));
            if (!isset($supply_material_price['base_price'])) {
                $base_price = SteelMaterialPriceSupply::where(['material_attr_id'=>$material_id,'cpid'=>$cpid])->value('base_price');
            } else {
                $base_price = $supply_material_price['base_price'];
            }


            if ($base_price <= 0) {
                return ['price'=>0,'base_price'=>0,'add_price'=> $stockinfo['supply_add_price']];
            } else {
                return [
                    'price'=>intval(mathfunc('add', $base_price, $stockinfo['supply_add_price'])),
                    'base_price'=>$base_price,
                    'add_price'=> $stockinfo['supply_add_price']
                ];
            }
        } else {
            //根据时间取价格--取当前时间最新价格--加价取最新加价
            $base_price=SteelMaterialPriceSupplyHistory::where(['material_attr_id'=>$material_id,'cpid'=>$cpid])
            ->where([['create_at','<',$time]])->orderBy('create_at','desc')->value('base_price');

            if ($base_price <= 0) {
                $supply_material_price = json_decode(Redis::get('supply_material_price_' . $material_id));
                if (!isset($supply_material_price['base_price'])) {
                    $base_price = SteelMaterialPriceSupply::where(['material_attr_id'=>$material_id,'cpid'=>$cpid])->value('base_price');
                } else {
                    $base_price = $supply_material_price['base_price'];
                }


                if ($base_price <= 0) {
                    return ['price'=>0,'base_price'=>0,'add_price'=> $stockinfo['supply_add_price']];
                } else {
                    return [
                        'price'=>intval(mathfunc('add', $base_price, $stockinfo['supply_add_price'])),
                        'base_price'=>$base_price,
                        'add_price'=> $stockinfo['supply_add_price']
                    ];
                }
                
            } else {
                return [
                    'price'=>intval(mathfunc('add', $base_price, $stockinfo['supply_add_price'])),
                    'base_price'=>$base_price,
                    'add_price'=> $stockinfo['supply_add_price']
                ];
            }
        }
    }

    /**
     * 获取指定时间区间基价平均值
     */
    public static function getpriceavg($material_id, $cpid, $stime, $etime){
        $base_price = SteelMaterialPriceSupplyHistory::where(['material_attr_id'=>$material_id,'cpid'=>$cpid])
        ->where([['create_at','>=',$stime],['create_at','<=',$etime]])
        ->avg('base_price');
        return intval($base_price);//价格分取整
    }

    /**
     * 计算采购订单相关统计
     */
    public static function getOrderSum($list){
        foreach($list as &$val){
            $suborder=$val['suborder'];
            $total_weight = array_column($suborder, 'total_weight');
            $val['total_weight']=array_sum($total_weight);


            $total_money = array_column($suborder, 'total_money');
            $val['total_money']=array_sum($total_money);


            $count = array_column($suborder, 'count');
            $val['count']=array_sum($count);

        }

        return $list;
    }
}
